Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Speed comparison: Passing data vector by name vs pointers in Mata | Stata

    I was experimenting with different uses of pointers. Turns out that in a larger dataset, pointer might conserve memory and hence offer some speed advantage. Those interested might find this code interesting. https://fintechprofessor.com/2021/12...in-mata-stata/
    Regards
    --------------------------------------------------
    Attaullah Shah, PhD.
    Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
    FinTechProfessor.com
    https://asdocx.com
    Check out my asdoc program, which sends outputs to MS Word.
    For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

  • #2
    Thank you for this exploration. You can get improved efficiency with both approaches by explicitly declaring variable types in the function arguments, and this can help attenuate differences between the two approaches.
    Code:
    version 17.0
    
    clear *
    
    mata:
    mata set matastrict on
    
    real scalar function notPointer(real colvector A) return(mean(A))
    
    real scalar function withPointer(pointer(real colvector) scalar b) return(mean(*b))
    
    void function testEm(| real scalar total_replications) {
    
        if (!args()) total_replications = 5
    
        real colvector A
        A = range(0, 100000000, .25)
    
        pointer(real colvector) scalar b
        b = &A
    
        timer_clear()
    
        real scalar replication
        for (replication=1; replication<=total_replications; replication++) {
    
            timer_on(1)
            (void) notPointer(A)
            timer_off(1)
    
            timer_on(2)
            (void) withPointer(b)
            timer_off(2)
    
        }
    
        timer()
    }
    
    testEm()
    
    end
    
    exit
    Running this code on my machine, usage of a pointer argument still offers a very slight time advantage in this use case.

    Have you looked into whether the mata set matafavor {space | speed} affects the relative difference between the two approaches?

    Comment


    • #3
      Thank you for your insightful reply. Results that I reported in the blog post are based on my office computer. On my home computer, the speed difference is no more significant. And when I do
      Code:
      mata: mata set matafavor speed
      , the pointer approach is slightly slower than passing a vector by name. I think the difference in speed of the two approaches varies significantly by CPU type. I am using Stata SE on both the computers.
      Regards
      --------------------------------------------------
      Attaullah Shah, PhD.
      Professor of Finance, Institute of Management Sciences Peshawar, Pakistan
      FinTechProfessor.com
      https://asdocx.com
      Check out my asdoc program, which sends outputs to MS Word.
      For more flexibility, consider using asdocx which can send Stata outputs to MS Word, Excel, LaTeX, or HTML.

      Comment

      Working...
      X